package com.itcam.tingshu.search.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.itcam.tingshu.album.client.AlbumInfoFeignClient;
import com.itcam.tingshu.album.client.CategoryFeignClient;
import com.itcam.tingshu.common.result.Result;
import com.itcam.tingshu.model.album.AlbumInfo;
import com.itcam.tingshu.model.album.BaseCategoryView;
import com.itcam.tingshu.search.service.ItemService;
import com.itcam.tingshu.user.client.UserInfoFeignClient;
import com.itcam.tingshu.vo.album.AlbumStatVo;
import com.itcam.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;


    /**
     * 根据专辑Id回显专辑数据
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> getItem(Long albumId) {
        // 创建map集合
        Map<String, Object> hashMap = new HashMap<>();
        // 远程调用
        // 异步编排
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            // 远程调用
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(albumInfoResult, "专辑结果集为空");
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "专辑为空");
            // 数据存储
            hashMap.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolExecutor);


        // 获取分类数据
        CompletableFuture<Void> cateCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            // 远程调用
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(categoryViewResult, "分类结果集为空");
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            Assert.notNull(baseCategoryView, "三级分类为空");
            // 存储数据
            hashMap.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor);

        // 获取主播信息
        CompletableFuture<Void> userInfoCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            // 远程调用
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
            Assert.notNull(userInfoVoResult, "主播结果集为空");
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            Assert.notNull(userInfoVo, "主播为空");
            // 存储数据
            hashMap.put("announcer", userInfoVo);
        }, threadPoolExecutor);

        // 获取专辑的统计信息
        CompletableFuture<Void> statVoCompletableFuture = CompletableFuture.runAsync(() -> {
            Result<AlbumStatVo> statVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(statVoResult, "专辑统计结果集为空");
            AlbumStatVo albumStatVo = statVoResult.getData();
            Assert.notNull(albumStatVo, "专辑统计为空");
            // 存储数据
            hashMap.put("albumStatVo", albumStatVo);
        }, threadPoolExecutor);

        // 多任务组合
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                cateCompletableFuture,
                userInfoCompletableFuture,
                statVoCompletableFuture
        ).join();

        // 返回数据
        return hashMap;
    }
}
